﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Model;
using FbKPI.Facade;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;
using System.Web.Script.Services;
using System.Web.Security;
using System.Web.SessionState;

namespace FBKPI.Serializer
{
    /// <summary>
    /// Summary description for LoginSerializer
    /// </summary>
    public class LoginSerializer : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string result = this.GetLoginData();
            context.Response.Write(result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string GetLoginData()
        {
            
            string Username = HttpContext.Current.Request.Params["Username"];
            string Password = HttpContext.Current.Request.Params["Password"];

            string userData = string.Empty;

            string result = "false";
            Employee data = new Employee();
            sysLogFacade sysLogFacade = new sysLogFacade();
            sysLog objSysLog = new sysLog();

            try
            {
                EmployeeFacade pf = new EmployeeFacade();
                data = pf.GetEmployeeLogin(Username, Password);


                if (data != null)
                {
                    result = "true";

                    userData = data.ID.ToString();
                    // Create a new ticket for user that has been authenticated.                    
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                        data.ID.ToString(),
                        DateTime.Now,
                        DateTime.Now.Date.AddDays(1),// .Now.AddMinutes(300),
                        false,
                        userData,
                        FormsAuthentication.FormsCookiePath);

                    // Create the cookie.
                    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName);
                    cookie.Value = FormsAuthentication.Encrypt(ticket);
                    cookie.Expires = ticket.Expiration;
                    HttpContext.Current.Response.Cookies.Add(cookie);
                    HttpContext.Current.Session["UserLoggedIn"] = userData;// data.ID.ToString();
                    objSysLog.Description = "Login sukses";

                }
            }
            catch (Exception ex)
            {
                
                objSysLog.Description = ex.Message;
            }
            objSysLog.LogEvent = "Login";
            objSysLog.Source = "LoginSerializer.ashx";
            objSysLog.CreatedBy = Username;
            objSysLog.CreatedDate = DateTime.Now;
            sysLogFacade.Save(objSysLog);
            var gridData = new
            {
                result,
                data
            };
            var JSONSerializer = new JavaScriptSerializer();
            return JSONSerializer.Serialize(gridData);
        }
    }
}